﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.Internal.Xml.XPath;
using System.Web.Mvc;

namespace MvcControlsToolkit
{
    public class Splitter: Container
    {
        /// <summary>
        /// Top or left content place
        /// </summary>
        public Splitter First { get; set; }

        /// <summary>
        /// Bottom or right content place
        /// </summary>
        public Splitter Second { get; set; }
        
        private HtmlHelper helper;
        public HtmlHelper Helper
        {
            get { return helper; }
            set 
            { 
                helper = value;
                if (First != null)
                    First.Helper = value;
                if (Second != null)
                    Second.Helper = value;
            }
        }

        public SplitterType Type
        {
            get
            {
                switch(Attributes["orientation"])
                {
                    case "v":
                        return SplitterType.Vertical;
                        break;
                    case "h":
                        return SplitterType.Horizontal;
                        break;
                }
                return SplitterType.Vertical;
            }
            set
            {
                switch(value)
                {
                    case SplitterType.Vertical:
                        SetString("orientation", "vertical");
                        break;
                    case SplitterType.Horizontal:
                        SetString("orientation", "horizontal");
                        break;
                }
            }
        }

        public Splitter(string name) : base(name){}

        public Splitter SetContent(ContentType contentType, string name, object objectModel = null, ViewDataDictionary viewData = null)
        {
            return base.SetContent(contentType, name, objectModel, viewData) as Splitter;
        }

        public override string GetJScript()
        {
            var result = new StringBuilder();

            if (First != null && Second != null)
            {
                foreach (var key in Attributes.Keys)
                {
                    result.AppendFormat(", {0}: {1}", key, Attributes[key]);
                }

                if (result.Length > 0)
                    result.Remove(0, 1);

                result.Append("});\r\n");

                result.Insert(0, "var "+Name+" = $(\"#" + Name + "\").split({");

                result.Append(First.GetJScript());
                result.Append(Second.GetJScript());
            }

            return result.ToString();
        }

        public override string GetHtml()
        {
            var result = new StringBuilder();

            result.Append(String.Format("<div id=\"{0}\">\r\n", Name));

            if(First == null && Second == null)
            {
                switch (ContentType)
                {
                    case ContentType.Static:
                        result.Append(HTMLContent);
                        break;
                    case ContentType.PartialView:
                        result.Append(ControlManager.RenderPartialToString(Helper, PartialViewName, PartialViewModel, PartialViewData));
                        break;
                    case ContentType.RemoteAction:
                        throw new NotImplementedException();
                        break;
                }
            }

            if (First != null && Second != null)
            {
                result.Append(First.GetHtml());
                result.Append(Second.GetHtml());
            }

            result.Append("</div>\r\n");

            return result.ToString();
        }
    }

    public enum SplitterType
    {
        Vertical,
        Horizontal
    }
}
